package MainTest;//内部类实现两个监听
import javax.swing.*;
//import MyDrawPanel.Button1Listener;

import java.awt.*;
import java.awt.event.*;
/**
 * this的用法:
 * 在一个类的方法或构造方法内部,用this关键字引用成员变量:this.test.
 * 在一个类的构造方法内部,用this关键字在自身构造方法中引用其他构造方法.
 * 在一个类的内部,this代表自身类的对象.
 * 在一个类的内部,使用this引用成员方法:this.max().
 * @author Zhirao Zhang
 **/
public class SimpleGui2 implements ActionListener {
    JButton button1,button2;
    JFrame frame;
    Label label1,label2,label3;

    //实现接口上的方法
    public static void main(String[] args) {
        SimpleGui2 gui = new SimpleGui2();
        gui.go();
    }
    public void go() {
        //frame框架操作
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //按钮操作
        button1 = new JButton("点老子");
        button2 = new JButton("别点老子");
        //监听添加到按钮上,this在这例子中所指为gui这个对象
        button1.addActionListener(new Button1Listener());
        button2.addActionListener(new Button2Listener());

        label1 = new Label("Where is east?");
        label2 = new Label("你猜为什么乱码 ?");
        //注释下面那行就会触发try-catch
        //label3 = new Label("try-catch test");
        //panel仪表板
        MyDrawPanel drawPanel = new MyDrawPanel();
        try {
            frame.getContentPane().add(BorderLayout.SOUTH,button1);
            frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
            frame.getContentPane().add(BorderLayout.EAST,label1);
            frame.getContentPane().add(BorderLayout.WEST,button2);
            frame.getContentPane().add(BorderLayout.NORTH,label3);
            frame.setSize(500,400);
            frame.setVisible(true);
        } catch (Exception e) {
            System.out.println("这里（try和catch）出了问题!");
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }


    class MyDrawPanel extends JPanel{
        public void  paintComponent(Graphics g){
            g.fillRect(0,0,this.getWidth(),this.getHeight());
    
            int Red = (int)(Math.random()*255);
            int Green = (int)(Math.random()*255);
            int Yellow = (int)(Math.random()*255);
    
            Color radomColor = new Color(Red,Green,Yellow);
            g.setColor(radomColor);
            g.fillOval(10,10,100,100);
            //Image image = new ImageIcon("autobackup.icon").getImage();
            //g.drawImage(image,3,4,this);
        }
    }
    class Button1Listener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            button1.setText("点过了");
            frame.repaint();
        }
    }
    class Button2Listener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            button2.setText("都说了别点老子!");
            frame.repaint();
        }
    }
}